import React, { Component } from 'react'
import constant from "../loan-setting/constant";
import { Button, Form, Input, Switch, message } from 'antd'
import { messages } from "share/common";
import InputLanguage from 'components/template/input-language'
import SubjectService from 'containers/finance-setting/subject-sheet/subject.service'
import { connect } from "react-redux";

@connect(mapStateToProps)
class SubjectRule extends Component {

  state = {
    saveLoading: false
  };

  handleSave = () => {
    this.props.form.validateFieldsAndScroll((err, value) => {
      if (!err) {
        const { record } = this.props.params, { close, loginUser, tenantInfo } = this.props;
        let i18n = value.accountSetDesc;
        value.accountSetDesc = i18n.name;
        value.i18n = {
          accountSetDesc: i18n.i18n
        };
        this.setState({ saveLoading: true });
        SubjectService.saveSubject(Object.assign({ tenantId: tenantInfo.id }, record, value), loginUser.userOID).then(() => {
          this.setState({ saveLoading: false }, () => {
            message.success(messages('common.operate.success')/*操作成功*/);
            close(true);
          });
        }).catch(e => {
          message.error(e.response.data.message);
          this.setState({ saveLoading: false });
        })
      }
    })
  };

  render() {
    const { formItemLayout } = constant;
    const { saveLoading } = this.state;
    const { close } = this.props, { record } = this.props.params;
    const { getFieldDecorator } = this.props.form;
    return (
      <div>
        <Form className={'new-form'}>
          <Form.Item
            label={messages('common.column.status')/*状态*/}
            {...formItemLayout}
          >
            {getFieldDecorator("enabled", {
              rules: [{ required: true }],
              valuePropName: "checked",
              initialValue: record.hasOwnProperty('enabled') ? record.enabled : true
            })(
              <Switch>{messages('finance-5.key48')/*启用temp*/}</Switch>
            )}
          </Form.Item>
          <Form.Item
            label={messages('finance-5.key54')/*科目表编码*/}
            {...formItemLayout}
            extra={messages('finance-5.key29')/*确定后不能修改*/}
          >
            {getFieldDecorator('accountSetCode',{
              rules: [
                { required: true, message: messages('common.please.input')/*请输入*/ },
                { max: 35, message: messages('common.max.characters.length',{arg1: 35})/*最多输入{arg1}个字符*/ }
              ],
              initialValue: record.accountSetCode
            })(
              <Input disabled={record.hasOwnProperty('id')} placeholder={messages('common.please.input')/*请输入*/}/>
            )}
          </Form.Item>
          <Form.Item
            label={messages('finance-5.key55')/*科目表名称*/}
            {...formItemLayout}
          >
            {getFieldDecorator('accountSetDesc', {
              rules: [
                { required: true, message: messages('common.please.input')/*请输入*/ }
              ],
              initialValue: { name: record.accountSetDesc, i18n: record.i18n ? record.i18n.accountSetDesc : [] }
            })(
              <InputLanguage
                placeholder={messages('common.max.characters.length',{arg1: 100})/*最多输入{arg1}个字符*/}
                maxLength={100}
              />
            )}
          </Form.Item>
          <div className='footer-operate'>
            <Button type='primary' onClick={this.handleSave} loading={saveLoading}>{messages('common.ok')/*确定*/}</Button>
            <Button onClick={() => close()}>{messages('common.cancel')/*取消*/}</Button>
          </div>
        </Form>
      </div>
    )
  }

}

function mapStateToProps(state) {
  return {
    loginUser: state.login.loginUser,
    tenantInfo: state.login.tenant
  }
}

export default Form.create()(SubjectRule);
